import pyOsi

#INITIALIZATION
theSI = {}
for solver in pyOsi.Solvers:
	theSI[solver] = pyOsi.getOsi(solver)
	if not theSI[solver]:
		print "WARNING: Solver %s is not available" % solver
		del theSI[solver]
if len(theSI) <= 0:
	print "ERROR: No solvers available"
	exit(-1)


#PROBLEM LOADING
for solver in theSI:
	if theSI[solver].readMps("p0033") > 0:
		print "WARNING: Solver %s is not able to load problem" % solver
		del theSI[solver]
if len(theSI) <= 0:
	print "ERROR: None of the solvers loaded the problem"
	exit(-1)
	
#PROBLEM SOLVING
for solver in theSI:
	theSI[solver].initialSolve()
	if ( theSI[solver].isProvenOptimal() ):
    		print "%s has found an optimal solution!" % solver
	else:
		print "WARNING %s hasn't found optimal solution." % solver
		del theSI[solver]
if len(theSI) <= 0:
	print "ERROR: None of the solvers has found an optimal solution!"
	exit(-1)
else:
	for s in theSI: 
		ncol = theSI[s].getNumCols()
		break

#COMPARING SOLUTIONS
objval = {}
solvect =  {}
for solver in theSI:
	objval[solver] = theSI[solver].getObjValue()
	solvect[solver] = theSI[solver].getColSolution()

obj_med = 0.0
obj_sq_med = 0.0
for s in theSI: 
	obj_med += objval[s]
	obj_sq_med += objval[s]**2

obj_med /= len(theSI)
obj_sq_med /= len(theSI)

sol_med = [ 0.0 for i in xrange(ncol) ]
sol_sq_med = [ 0.0 for i in xrange(ncol) ]
for i in xrange(ncol):
	sol_med[i] = 0.0
	sol_sq_med[i] = 0.0
	for s in theSI: 
		sol_med[i] += solvect[solver][i]
		sol_sq_med[i] += solvect[solver][i]**2

	sol_med[i] /= len(theSI)
	sol_sq_med[i] /= len(theSI)	

print "Computation done for %d solver " % len(theSI)
print "Avegare Objective value: %f       s.d. : %f " % ( obj_med , obj_sq_med - obj_med**2 ) 
for i in xrange(ncol):
	print "[%d]  %f : s.d. : %f" % ( i , sol_med[i], sol_sq_med[i] - sol_med[i]**2 )



